package com.example.assetmanagement.service.impl;

import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.OperationLog;
import com.example.assetmanagement.mapper.OperationLogMapper;
import com.example.assetmanagement.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {

    private final OperationLogMapper logMapper;

    @Override
    public PageResponse<OperationLog> list(PageRequest pageRequest, String module, String operationType, Long userId) {
        List<OperationLog> list = logMapper.selectPage(pageRequest.getOffset(), pageRequest.getSize(), module, operationType, userId);
        long total = logMapper.count(module, operationType, userId);
        return new PageResponse<>(total, list);
    }

    @Override
    public OperationLog getById(Long id) {
        return logMapper.selectById(id);
    }

    @Override
    @Transactional
    public void add(OperationLog log) {
        logMapper.insert(log);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        logMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) return;
        logMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void clear() {
        logMapper.clear();
    }

    @Override
    public Map<String, Object> stats() {
        return logMapper.stats();
    }

    @Override
    public List<Map<String, Object>> operationTypeStats() {
        return logMapper.operationTypeStats();
    }

    @Override
    public List<Map<String, Object>> moduleStats() {
        return logMapper.moduleStats();
    }
} 